package cs.rsa.ts14.juliet;

import cs.rsa.ts14.framework.LineType;
import cs.rsa.ts14.framework.LineTypeClassifierStrategy;

public class StandardLineTypeClassifierStrategy implements LineTypeClassifierStrategy {
	
	LineType lastSeen;
	String lastLine;
	
	@Override
	public LineType classify(String line) {
		lastSeen = LineType.INVALID_LINE;
		lastLine = line;
		
		if (line.startsWith("#")) {
			lastSeen = LineType.COMMENT_LINE;
		}
		else if (line.matches("\\s*")) {
			lastSeen = LineType.EMPTY_LINE;
		}
		else if (line.matches("^Week\\s+([1-9]|[1-4][0-9]|5[0-2])\\s+:\\s+[0-5]\\s+:\\s+[0-5]\\s*")) {
			String[] result = line.split("\\s+");
			int sum = Integer.parseInt(result[3]) + Integer.parseInt(result[5]);
			if (/*sum >= 0 &&*/ sum <= 5) // first condition "removed" because the sum can never be under zero ;)
				lastSeen = LineType.WEEK_SPECIFICATION;
		}
		else if (line.matches("\\w+\\s+[=]\\s+[0-9]+(.[0-9])?")) {
			lastSeen = LineType.ASSIGNMENT_LINE;	
		}
		else if (line.matches("^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\\s+(Bi|Ca|Pu|Tr|No|Ho)\\s*.*")) {
			lastSeen = LineType.WEEKDAY_SPECIFICATION;	
		}
		else if (line.matches("^\\s+[A-z][A-z0-9_]+\\s+([A-z][A-z0-9_]*|[-])\\s+(([0-9]|1[0-9]|2[0-3])\\.[5]{1}|([0-9]|1[0-9]|2[0-4]))(\\s+[A-z][A-z0-9_ ]+)?")) {
			lastSeen = LineType.WORK_SPECIFICATION;	
		}
	    return lastSeen;
	}

	@Override
	public String lastError() {
		if ( lastSeen == LineType.INVALID_LINE ) 
			return "Line: " + lastLine + " is invalid";
		return "No error";
	}

}
